package com.it.product.comtroller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.it.product.domain.ProductUnit;
import com.it.product.service.IProductUnitService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/19 23:00 周四
 * description:
 */
@Tag(name = "商品单位")
@RequestMapping("/productUnit")
@RestController
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService;

    /**
     * 分页查询商品单位
     * @param pageNum 起始坐标
     * @param pageSize 最大页数
     * @param productUnit 查询条件
     * @return
     */
    @Operation(summary = "获取分页列表")
    @GetMapping("/list")
    public TableDataInfo findPage(@RequestParam(value = "pageNum",defaultValue = "0",required = true) Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "0",required = true) Integer pageSize,
                                  ProductUnit productUnit) {
        //使用mybatis_plus的分页
      //  PageHelper.startPage(pageNum,pageSize);
        IPage<ProductUnit> productUnitPage = new Page<>(pageNum,pageSize);
        IPage page= productUnitService.selectProductUnitPage(productUnitPage,productUnit);
        return getDataTable(page);
    }

    /**
     * 获取商品单位详细信息
     */
    @Operation(summary = "获取商品单位详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        ProductUnit byId = productUnitService.getById(id);
        return success(byId);
    }

    /**
     * 新增商品
     * @param productUnit
     * @return
     */
    @Operation(summary = "新增商品信息")
    @PostMapping
    public AjaxResult save(@RequestBody @Validated ProductUnit productUnit){
        boolean b = productUnitService.saveOrUpdate(productUnit);
        return success(b? 1:0);
    }

    /**
     * 修改商品
     * @param productUnit
     * @return
     */
    @Operation(summary = "修改商品")
    @PutMapping
    public  AjaxResult updateProductUnit(@Validated @RequestBody ProductUnit productUnit){
        boolean b = productUnitService.saveOrUpdate(productUnit);
        return success(b? 1:0);
    }

    /**
     * 批量删除
     * @param idlist 多个id
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/{ids}")
    public  AjaxResult remove(@Validated @PathVariable("ids")List<Long> idlist){
        boolean b = productUnitService.removeBatchByIds(idlist);
        return success(b? 1:0);
    }

}